package com.example.android;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.android.bean.AddCar;
import com.example.android.utils.api.BackApi;
import com.example.android.utils.BaseModel;
import com.example.android.utils.NetUtil;

import java.util.Objects;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class carinfoFragment extends Fragment {
    private static final String ARG_PARAM1 = "id";

    private String mParam1;

    public carinfoFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_carinfo, container, false);
        NetUtil.INSTANCE.get(NetUtil.INSTANCE.getBaseUrlDev()).create(BackApi.class)
                .carInfo(Long.valueOf(mParam1)).enqueue(new Callback<BaseModel<AddCar>>() {
            @Override
            public void onResponse(Call<BaseModel<AddCar>> call, Response<BaseModel<AddCar>> response) {
                assert response.body() != null;
                if (response.body().getCode() == 200) {
                    //车牌
                    AddCar data = response.body().getData();
                    EditText carpd = root.findViewById(R.id.editTextTextPersonName);
                    carpd.setText(data.getPlaterNumber());
                    //车型
                    EditText carpn = root.findViewById(R.id.carpn);
                    carpn.setText(data.getBrand());
                    //取车地址
                    EditText carAddress = root.findViewById(R.id.carAddress);
                    carAddress.setText(data.getAddress());
                    // 车颜色
                    EditText color = root.findViewById(R.id.carcolor);
                    color.setText(data.getColor());
                    //  设置车照片
                    if (data.getImage1() != null && !Objects.equals(data.getImage1(), "")) {
                        Glide.with(getContext()).load(data.getImage1()).into((ImageView) root.findViewById(R.id.imageView));

                    }
                    if (data.getImage2() != null && !Objects.equals(data.getImage2(), "")) {
                        Glide.with(getContext()).load(data.getImage2()).into((ImageView) root.findViewById(R.id.imageView3));

                    }
                    if (data.getImage3() != null && !Objects.equals(data.getImage3(), "")) {
                        Glide.with(getContext()).load(data.getImage3()).into((ImageView) root.findViewById(R.id.imageView2));

                    }
                    if (data.getImage4() != null && !Objects.equals(data.getImage4(), "")) {
                        Glide.with(getContext()).load(data.getImage4()).into((ImageView) root.findViewById(R.id.imageView4));

                    }
                }
            }

            @Override
            public void onFailure(Call<BaseModel<AddCar>> call, Throwable t) {

            }
        });

        return root;
    }
}